<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <button id="btn">开关灯</button>
    <script>
        // 获取元素
        const btn = document.querySelector('#btn');
        const body = document.body;

        // 开关变量
        let flag = true;

        // 点击事件
        btn.onclick = function() {
            if (flag) {
                body.style.backgroundColor = '#000';
                flag = false;
            } else {
                body.style.backgroundColor = '#fff';
                flag = true;
            }
        }
    </script>
</body>

</html>